from flask import Blueprint, render_template, request, jsonify
from flask_login import login_required, current_user
from app.services.message_service import MessageService

bp = Blueprint('message', __name__, url_prefix='/api')
message_service = MessageService()

@bp.route('/message')
@login_required
def list_messages():
    try:
        sort_by = request.args.get('sort', 'time')
        page = request.args.get('page', 1, type=int)
        per_page = request.args.get('per_page', 20, type=int)
        
        messages = message_service.get_messages(sort_by=sort_by, user_id=current_user.id, page=page, per_page=per_page)
        return jsonify({
            'code': 200,
            'msg': 'success',
            'data': messages
        })
    except Exception as e:
        return jsonify({
            'code': 500,
            'msg': str(e),
            'data': None
        })

@bp.route('/message/create', methods=['POST'])
@login_required
def create_message():
    try:
        data = request.get_json()
        if not data:
            return jsonify({'code': 400, 'msg': '无效的请求数据', 'data': None})
        
        content = data.get('content')
        images = data.get('images', [])
        
        if not content:
            return jsonify({'code': 400, 'msg': '留言内容不能为空', 'data': None})
        
        message_id = message_service.create_message(current_user.id, content, images)
        return jsonify({
            'code': 200,
            'msg': '发表成功',
            'data': {'message_id': message_id}
        })
    except Exception as e:
        return jsonify({
            'code': 500,
            'msg': str(e),
            'data': None
        })

@bp.route('/message/<int:message_id>/comment', methods=['POST'])
@login_required
def create_comment(message_id):
    try:
        data = request.get_json()
        if not data:
            return jsonify({'code': 400, 'msg': '无效的请求数据', 'data': None})
        
        content = data.get('content')
        parent_id = data.get('parent_id')
        
        if not content:
            return jsonify({'code': 400, 'msg': '评论内容不能为空', 'data': None})
        
        comment_id = message_service.create_comment(message_id, current_user.id, content, parent_id)
        return jsonify({
            'code': 200,
            'msg': '评论成功',
            'data': {'comment_id': comment_id}
        })
    except Exception as e:
        return jsonify({
            'code': 500,
            'msg': str(e),
            'data': None
        })

@bp.route('/message/<int:message_id>/like', methods=['POST'])
@login_required
def toggle_like(message_id):
    try:
        result = message_service.toggle_message_like(message_id, current_user.id)
        return jsonify({
            'code': 200,
            'msg': 'success',
            'data': result
        })
    except Exception as e:
        return jsonify({
            'code': 500,
            'msg': str(e)
        })

@bp.route('/message/<int:message_id>')
@login_required
def get_message(message_id):
    try:
        message = message_service.get_message_by_id(message_id, current_user.id)
        if message:
            return jsonify({
                'code': 200,
                'msg': 'success',
                'data': message
            })
        return jsonify({
            'code': 404,
            'msg': '消息不存在',
            'data': None
        })
    except Exception as e:
        return jsonify({
            'code': 500,
            'msg': str(e),
            'data': None
        })